iT邦幫忙

2024 iThome 鐵人賽

DAY 21
0

這篇我們實作PlayerChessmanMoveData中剩下的方法


getArrCastlingMoveData()

我們判斷國王與城堡之間的格子為空白格,且國王與城堡皆為位移動過的狀態時,返回可易位的格子

public ArrayList getArrCastlingMoveData(HashMap<String,Object> chessboardData, int position,HashMap<String,Object> castlingData){
        ArrayList canCastlingList = new ArrayList<>();
        if(player.equals("1")){
            for(int i=0;i<64;i++){
                if(i == 60 && i == position){
                    if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(own+"K") && castlingData.get("e1").equals(true)
                            && chessboardData.get(playerChessboardData.getStringBoardData(56)).equals(own+"R") && castlingData.get("a1").equals(true)){
                        if(chessboardData.get(playerChessboardData.getStringBoardData(57)).equals("  ")
                                && chessboardData.get(playerChessboardData.getStringBoardData(58)).equals("  ")
                                && chessboardData.get(playerChessboardData.getStringBoardData(59)).equals("  ")){
                            canCastlingList.add(58);
                        }
                    }

                    if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(own+"K") && castlingData.get("e1").equals(true)
                            && chessboardData.get(playerChessboardData.getStringBoardData(63)).equals(own+"R") && castlingData.get("h1").equals(true)){
                        if(chessboardData.get(playerChessboardData.getStringBoardData(61)).equals("  ")
                                && chessboardData.get(playerChessboardData.getStringBoardData(62)).equals("  ")){
                            canCastlingList.add(62);
                        }
                    }
                }
            }
        }
        else if (player.equals("2")) {
            for(int i=0;i<64;i++){
                if(i == 59 && i == position){
                    if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(own+"K") && castlingData.get("e8").equals(true)
                            && chessboardData.get(playerChessboardData.getStringBoardData(63)).equals(own+"R") && castlingData.get("a8").equals(true)){
                        if(chessboardData.get(playerChessboardData.getStringBoardData(61)).equals("  ")
                                && chessboardData.get(playerChessboardData.getStringBoardData(62)).equals("  ")
                                && chessboardData.get(playerChessboardData.getStringBoardData(60)).equals("  ")){
                            canCastlingList.add(61);
                        }
                    }

                    if(chessboardData.get(playerChessboardData.getStringBoardData(i)).equals(own+"K") && castlingData.get("e8").equals(true)
                            && chessboardData.get(playerChessboardData.getStringBoardData(56)).equals(own+"R") && castlingData.get("h8").equals(true)){
                        if(chessboardData.get(playerChessboardData.getStringBoardData(57)).equals("  ")
                                && chessboardData.get(playerChessboardData.getStringBoardData(58)).equals("  ")){
                            canCastlingList.add(57);
                        }
                    }
                }
            }
        }
        return canCastlingList;
    }

getArrSpMoveData()

將在初始位置的士兵返回可走2格的走法

public ArrayList getArrSpMoveData(int position){
        ArrayList canSpMoveList = new ArrayList<>();
        for(int i=0;i<64;i++){
            if(i >= 48 && i <= 55 && position == i){
                canSpMoveList.add(i - 16);
            }
        }
        return canSpMoveList;
    }

getArrEpMoveData()

吃過路兵的規則較複雜,我們利用士兵狀態的陣列(pawnMove)來判斷該士兵是否為第一次行走2格,若是則產生可以吃棋的走法

public ArrayList getArrEpMoveData(HashMap<String,Object> chessboardData, HashMap<String,Object> pawnMove1,HashMap<String,Object> pawnMove2, int position){
        int up = position/8;
        int left = position%8;
        int down = 7-position/8;
        int right = 7-position%8;
        ArrayList canEpMoveList = new ArrayList<>();
        if(player.equals("1")){
            if(position >= 24 && position <= 31){
                if(left == 0 && right != 0){
                    if(chessboardData.get(playerChessboardData.getStringBoardData(position+1)).equals(other+"P")
                            && pawnMove2.get(playerChessboardData.getStringBoardData(position+1)).equals(true)){
                        canEpMoveList.add(position-7);
                    }
                }else if(left != 0 && right == 0){
                    if(chessboardData.get(playerChessboardData.getStringBoardData(position-1)).equals(other+"P")
                            && pawnMove2.get(playerChessboardData.getStringBoardData(position-1)).equals(true)){
                        canEpMoveList.add(position-9);
                    }
                }else if(left !=0 && right != 0){
                    if(chessboardData.get(playerChessboardData.getStringBoardData(position-1)).equals(other+"P")
                            && pawnMove2.get(playerChessboardData.getStringBoardData(position-1)).equals(true)){
                        canEpMoveList.add(position-9);
                    }
                    if(chessboardData.get(playerChessboardData.getStringBoardData(position+1)).equals(other+"P")
                            && pawnMove2.get(playerChessboardData.getStringBoardData(position+1)).equals(true)){
                        canEpMoveList.add(position-7);
                    }
                }
            }
        }
        else if(player.equals("2")){
            if(position >= 24 && position <= 31){
                if(left == 0 && right != 0){
                    if(chessboardData.get(playerChessboardData.getStringBoardData(position+1)).equals(other+"P")
                            && pawnMove1.get(playerChessboardData.getStringBoardData(position+1)).equals(true)){
                        canEpMoveList.add(position-7);
                    }
                }else if(left != 0 && right == 0){
                    if(chessboardData.get(playerChessboardData.getStringBoardData(position-1)).equals(other+"P")
                            && pawnMove1.get(playerChessboardData.getStringBoardData(position-1)).equals(true)){
                        canEpMoveList.add(position-9);
                    }
                }else if(left !=0 && right != 0){
                    if(chessboardData.get(playerChessboardData.getStringBoardData(position-1)).equals(other+"P")
                            && pawnMove1.get(playerChessboardData.getStringBoardData(position-1)).equals(true)){
                        canEpMoveList.add(position-9);
                    }
                    if(chessboardData.get(playerChessboardData.getStringBoardData(position+1)).equals(other+"P")
                            && pawnMove1.get(playerChessboardData.getStringBoardData(position+1)).equals(true)){
                        canEpMoveList.add(position-7);
                    }
                }
            }
        }
        return canEpMoveList;
    }

這樣我們便完成了所有棋子行走的方法,下篇我們來實作判斷棋子到指定格中間是否有棋子阻擋


上一篇
【DAY 20】activity - Player1ChessGame - getArrChessmanMoveData
下一篇
【DAY 22】activity - Player1ChessGame - isEnableMove
系列文
基於Firebase整合生成式AI研究開發雙人國際象棋系統(Based on Firebase and AI to research chess system)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言